home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / rbbs_pc / laston13.zip / LASTON.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  14KB  |  529 lines

  1. /*  LASTON.C
  2.  *---------------------------------------------------------------------------
  3.  *
  4.  *  RBBS Callers File Lister
  5.  *
  6.  *  11-04-91
  7.  *
  8.  */
  9. #include <stdio.h>
  10. #include <io.h>
  11. #include <alloc.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15.  
  16. typedef unsigned int uint;
  17. typedef unsigned char uchar;
  18.  
  19. #define TRUE    1
  20. #define FALSE   0
  21.  
  22. #define BLUE    ""
  23. #define RED     ""
  24. #define WHITE   ""
  25. #define YELLOW  ""
  26. #define CYAN    ""
  27.  
  28. struct LStruct
  29.    {
  30.    char       users_name[32];
  31.    char       city_state[25];
  32.    char       date_time[20];
  33.    };
  34.  
  35. FILE  *fpcall;
  36. uint  recs_in_file;
  37.  
  38. char  *mix_case(char * );
  39. char  *trim(char * );
  40. char  *getrec(int ,char * );
  41.  
  42. void main(int argc ,char *argv[] )
  43. {
  44.    int    i, len;
  45.    char   one_rec[129];
  46.    char   output_dir[80];
  47.    uint   max_callers = 5;
  48.    uint   num_callers = 0;
  49.    uint   num_excluded = 0;
  50.    int    name_len = -1;
  51.    int    city_len = -1;
  52.    int    date_len = -1;
  53.    int    same_caller;
  54.    int    display_dupes = FALSE;
  55.    struct LStruct *last_on;
  56.    char   excluded[20][32];
  57.    char   *color1_default = RED;
  58.    char   *color2_default = YELLOW;
  59.    char   *color3_default = WHITE;
  60.    char   *color4_default = CYAN;
  61.    char   *color5_default = BLUE;
  62.  
  63.    if (argc < 2 )
  64.       {
  65.       printf("Usage: LASTON <Callers> [/ODx] [/NCx] [/Xn] [/DD]\n" );
  66.       printf("              [/FCx] [/TCx] [/UCx] [/CCx] [/DCx]\n" );
  67.       printf("       /ODx - Write files to directory x\n" );
  68.       printf("       /NCx - Display x callers (defaults to 5)\n" );
  69.       printf("       /Xn  - Exclude callers with the name n\n" );
  70.       printf("                Use a '_' in place of a ' ' in name, i.e. /XTom_Collins\n" );
  71.       printf("                If n begins with an @, it is opened as a list of names\n" );
  72.       printf("                to exclude, i.e. /X@BADNAMES.TXT\n" );
  73.       printf("       /DD  - Display duplicate callers (default is to not display 'em)\n" );
  74.       printf("       /FC  - Set frame color string to x\n" );
  75.       printf("                LASTON puts this between a ESC '[' and an 'm'\n" );
  76.       printf("                i.e. /FC1;37 results in the ANSI string ESC [1;37m\n" );
  77.       printf("       /TC  - Set title color string to x\n" );
  78.       printf("       /UC  - Set user's name color string to x\n" );
  79.       printf("       /CC  - Set user's City/State color string to x\n" );
  80.       printf("       /DC  - Set user's date on color string to x\n" );
  81.       printf("\n" );
  82.       printf("Ex: LASTON c:\\rbbs\\callers /NC10 /XTom_Collins /dd /CC1;34\n" );
  83.       return;
  84.       }
  85.  
  86.    printf("LASTON v1.30 - Super Dooper RBBS Last Callers List Maker, by Tom Collins\n\n" );
  87.  
  88.    fpcall = fopen(strupr(argv[1] ) ,"rb" );
  89.    if (fpcall == NULL )
  90.       {
  91.       printf("Can't Find Callers File %s... Aborted.\n" ,argv[1] );
  92.       return;
  93.       }
  94.  
  95.    output_dir[0] = '\0';
  96.    for (i = 2; i < argc; i++ )
  97.       {
  98.       if (strncmp(strupr(argv[i] ) ,"/NC" ,3 ) == 0 )
  99.          {
  100.          max_callers = atoi(&argv[i][3] );
  101.          if (max_callers < 1 || max_callers > 25 )
  102.             {
  103.             max_callers = 5;
  104.             }
  105.          }
  106.       else if (strncmp(argv[i] ,"/OD" ,3 ) == 0 )
  107.          {
  108.          strcpy(output_dir ,&argv[i][3] );
  109.          len = strlen(output_dir );
  110.          if (len != 0 && output_dir[len-1] != ':' && output_dir[len-1] != '\\' )
  111.             {
  112.             strcat(output_dir ,"\\" );
  113.             }
  114.          }
  115.       else if (strncmp(argv[i] ,"/DD" ,3 ) == 0 )
  116.          {
  117.          display_dupes = TRUE;
  118.          }
  119.       else if (strncmp(argv[i] ,"/X" ,2 ) == 0 && num_excluded < 20 )
  120.          {
  121.          int j;
  122.  
  123.          if (argv[i][2] == '@' )
  124.             {
  125.             int  x;
  126.             char temp[80];
  127.             FILE *fp;
  128.  
  129.             strupr(argv[i] );
  130.             fp = fopen(&argv[i][3] ,"rt" );
  131.             if (fp == NULL )
  132.                {
  133.                printf("Can't Find Name Exclude List %s...\n" ,&argv[i][3] );
  134.                return;
  135.                }
  136.             else
  137.                {
  138.                while (fgets(temp ,79 ,fp ) != NULL )
  139.                   {
  140.                   x = strlen(temp );
  141.                   if (x != 0 && temp[x-1] == '\n' )
  142.                      temp[--x] = '\0';
  143.                   while (x >= 0 )
  144.                      {
  145.                      if (temp[x] == '_' )
  146.                         temp[x] = ' ';
  147.                      x--;
  148.                      }
  149.                   strcpy(excluded[num_excluded] ,mix_case(trim(temp ) ) );
  150.                   if (++num_excluded == 20 )
  151.                      break;
  152.                   }
  153.                fclose(fp );
  154.                }
  155.             }
  156.          else
  157.             {
  158.             for (j = 2; argv[i][j] != '\0'; j++ )
  159.                {
  160.                if (argv[i][j] == '_' )
  161.                   {
  162.                   argv[i][j] = ' ';
  163.                   }
  164.                }
  165.             strcpy(excluded[num_excluded] ,mix_case(&argv[i][2] ) );
  166.             num_excluded++;
  167.             }
  168.          }
  169.       else if (argv[i][0] == '/' && argv[i][2] == 'C' &&
  170.                strchr("FTUCD" ,argv[i][1] ) != NULL )
  171.          {
  172.          char c;
  173.          int j;
  174.  
  175.          j = strlen(argv[i] );
  176.          c = argv[i][1];
  177.  
  178.          strcpy(&argv[i][2] ,&argv[i][3] );
  179.  
  180.          argv[i][0]   = '\x1B';
  181.          argv[i][1]   = '[';
  182.          argv[i][j-1] = 'm';
  183.          argv[i][j]   = '\0';
  184.  
  185.          switch (c )
  186.             {
  187.             case 'F': color1_default = argv[i]; break;
  188.             case 'T': color2_default = argv[i]; break;
  189.             case 'U': color3_default = argv[i]; break;
  190.             case 'C': color4_default = argv[i]; break;
  191.             case 'D': color5_default = argv[i]; break;
  192.             }
  193.          }
  194.       }
  195.  
  196.    last_on = (struct LStruct *) calloc(sizeof(struct LStruct ) ,max_callers );
  197.  
  198.    recs_in_file = (uint) (filelength(fileno(fpcall ) ) / 64L);
  199.  
  200.    printf("Reading %s... " ,argv[1] );
  201.  
  202.    for (i = recs_in_file-1; i >= 0 && num_callers < max_callers; i-- )
  203.       {
  204.       same_caller = FALSE;
  205.  
  206.       /*
  207.        * Read a callers record
  208.        */
  209.       if (getrec(i ,one_rec ) == NULL )
  210.          {
  211.          break;
  212.          }
  213.       if (one_rec[0] != ' ' )
  214.          {
  215.          char *p1, *p2, *p3;
  216.  
  217.          one_rec[64] = '\0';
  218.          strlwr(one_rec );
  219.  
  220.          /*
  221.           * If the current record is a header, read the
  222.           * previous record, too.
  223.           */
  224.          if (((p1 = strstr(one_rec ,"on at" ) ) == NULL ) || (--i < 0 ) )
  225.             {
  226.             continue;
  227.             }
  228.          *p1 = '\0';
  229.  
  230.          strcpy(last_on[num_callers].users_name ,mix_case(trim(one_rec ) ) );
  231.  
  232.          if (num_callers != 0 )
  233.             {
  234.             if (strcmp(last_on[num_callers].users_name ,
  235.                        last_on[num_callers-1].users_name ) == 0 )
  236.                {
  237.                same_caller = TRUE;
  238.                }
  239.             }
  240.  
  241.          if (getrec(i ,&one_rec[64] ) == NULL )
  242.             break;
  243.  
  244.          one_rec[128] = '\0';
  245.          strlwr(p1+1 );
  246.  
  247.          if (strstr(p1+1 ,"** logon denied **" ) != NULL )
  248.             {
  249.             continue;
  250.             }
  251.          else if (same_caller && !display_dupes )
  252.             {
  253.             continue;
  254.             }
  255.          else if (num_excluded != 0 )
  256.             {
  257.             int i, found = FALSE;
  258.  
  259.             for (i = 0; i < num_excluded; i++ )
  260.                {
  261.                if (strcmp(last_on[num_callers].users_name ,excluded[i] ) == 0 )
  262.                   {
  263.                   found = TRUE;
  264.                   break;
  265.                   }
  266.                }
  267.             if (found )
  268.                continue;
  269.             }
  270.  
  271.          if ((p2 = strstr(p1+5 ,"from" ) ) == NULL )
  272.             {
  273.             continue;
  274.             }
  275.  
  276.          *p2 = '\0';
  277.          strcpy(last_on[num_callers].date_time ,strupr(trim(p1+5 ) ) );
  278.  
  279.          if ((p3 = strstr(p2+4 ,"baud" ) ) == NULL )
  280.             {
  281.             if ((p3 = strstr(p2+4 ,"bps" ) ) == NULL )   /* v1.30 */
  282.                {
  283.                continue;
  284.                }
  285.             }
  286.  
  287.          /*
  288.           * Find the last comma before the baud rate
  289.           */
  290.          while (p3 > p2+4 )
  291.             {
  292.             if (*p3 == ',' )
  293.                break;
  294.             p3--;
  295.             }
  296.  
  297.          if (p3 == p2+4 )
  298.             {
  299.             continue;
  300.             }
  301.  
  302.          *p3 = '\0';
  303.          strcpy(last_on[num_callers].city_state ,mix_case(trim(p2+4 ) ) );
  304.  
  305.          num_callers++;
  306.          }
  307.       }
  308.  
  309.    fclose(fpcall );
  310.  
  311.    for (i = 0; i < num_callers; i++)
  312.       {
  313.       len = strlen(last_on[i].users_name );
  314.       if (len > name_len )
  315.          {
  316.          name_len = len;
  317.          }
  318.       len = strlen(last_on[i].city_state );
  319.       if (len > city_len )
  320.          {
  321.          city_len = len;
  322.          }
  323.       len = strlen(last_on[i].date_time );
  324.       if (len > date_len )
  325.          {
  326.          date_len = len;
  327.          }
  328.       }
  329.  
  330.    if (num_callers == 0 )
  331.       {
  332.       printf("No Previous Callers... Done.\n" );
  333.       return;
  334.       }
  335.  
  336.    printf("Writing Output Files... " );
  337.  
  338.    for (i = 0; i < 3; i++)
  339.       {
  340.       int    j, k;
  341.       FILE   *fpout;
  342.       char   path[80];
  343.       char   temp[40];
  344.       char   left_margin[40];
  345.       char   single_line[81];
  346.       char   double_line[81];
  347.       char   fmt[80];
  348.       char   temp1[10];
  349.       char   temp2[10];
  350.       char   temp3[10];
  351.       char   *color1 = "";
  352.       char   *color2 = "";
  353.       char   *color3 = "";
  354.       char   *color4 = "";
  355.       char   *color5 = "";
  356.       char   *filename = "LASTONG";
  357.       char   single_line_char = '\xC4';
  358.       char   double_line_char = '\xCD';
  359.  
  360.       switch (i )
  361.          {
  362.          case 0:
  363.             single_line_char = '-';
  364.             double_line_char = '=';
  365.             filename = "LASTON";
  366.             break;
  367.          case 1:
  368.             break;
  369.          case 2:
  370.             color1 = color1_default;
  371.             color2 = color2_default;
  372.             color3 = color3_default;
  373.             color4 = color4_default;
  374.             color5 = color5_default;
  375.             filename = "LASTONC";
  376.             break;
  377.          }
  378.  
  379.       strupr(strcat(strcpy(path ,output_dir ) ,filename ) );
  380.  
  381.       fpout = fopen(path ,"wt" );
  382.       if (fpout == NULL )
  383.          {
  384.          printf("\nCan't Open Output File %s...\n" ,path );
  385.          return;
  386.          }
  387.  
  388.       len = name_len + city_len + date_len + 8;
  389.  
  390.       memset(left_margin ,' ' ,(79 - len)/2 ); left_margin[(79 - len)/2] = '\0';
  391.       memset(single_line ,single_line_char ,len ); double_line[len] = '\0';
  392.       memset(double_line ,double_line_char ,len ); single_line[len] = '\0';
  393.  
  394.       sprintf(fmt ,"%s%%-%ss    %s%%-%ss    %s%%%ss%s\n" ,
  395.               color3 ,itoa(name_len ,temp1 ,10 ) ,
  396.               color4 ,itoa(city_len ,temp2 ,10 ) ,
  397.               color5 ,itoa(date_len ,temp3 ,10 ) ,
  398.               i == 2? "" : "" );
  399.  
  400.       fprintf(fpout ,"\n%s%s%s%s\n" ,left_margin ,color1 ,double_line ,
  401.               i == 2? "" : "" );
  402.  
  403.       if (num_callers == 1 )
  404.          strcpy(temp ,"The Last Caller Was..." );
  405.       else
  406.          sprintf(temp ,"The Last %i Callers Were..." ,num_callers );
  407.  
  408.       fprintf(fpout ,"%s%s" ,left_margin ,color2 );
  409.       k = len - strlen(temp);
  410.  
  411.       for (j = 0; j < k/2; j++) fputc(' ' ,fpout );
  412.       fputs(temp ,fpout );
  413.       for (j = 0; j < (k - k/2); j++) fputc(' ' ,fpout );
  414.  
  415.       fprintf(fpout ,"%s\n%s%s%s%s\n" ,i == 2? "" : "" ,
  416.               left_margin ,color1 ,single_line ,i == 2? "" : "" );
  417.  
  418.       for (j = 0; j < num_callers; j++)
  419.          {
  420.          fputs(left_margin ,fpout );
  421.          fprintf(fpout ,fmt ,last_on[j].users_name ,
  422.                              last_on[j].city_state ,
  423.                              last_on[j].date_time );
  424.          }
  425.  
  426.       fprintf(fpout ,"%s%s%s%s\n{PB\n" ,left_margin ,color1 ,single_line ,
  427.               i == 2? "" : "" );
  428.  
  429.       fclose(fpout );
  430.       }
  431.  
  432.    printf("Done.\n" );
  433. }
  434.  
  435. /*
  436.  *----------------------------------------------------------------------------
  437.  *
  438.  */
  439. char *getrec(int rec_num ,char *rec )
  440. {
  441.    int   x, recs_to_read;
  442.    static int   first;
  443.    static char  *buffer = NULL;
  444.  
  445.    if (buffer == NULL)
  446.       {
  447.       buffer = malloc(32000 );
  448.       if (buffer == NULL )
  449.          {
  450.          printf("Out of Memory.\n" );
  451.          exit(0 );
  452.          }
  453.       first = recs_in_file;
  454.       }
  455.  
  456.    if (rec_num < first )
  457.       {
  458.       first -= 500;
  459.       recs_to_read = 500;
  460.       if (first < 0 )
  461.          {
  462.          recs_to_read += first;
  463.          first = 0;
  464.          }
  465.       fseek(fpcall ,((long)first)*64L ,SEEK_SET );
  466.       if (fread(buffer ,64 ,recs_to_read ,fpcall ) != recs_to_read )
  467.          return(NULL );
  468.       }
  469.    x = (rec_num - first ) * 64;
  470.    memcpy(rec ,&buffer[x] ,64 );
  471.  
  472.    return(rec );
  473. }
  474.  
  475. /*
  476.  *----------------------------------------------------------------------------
  477.  *
  478.  */
  479. char *mix_case(char *s )
  480. {
  481.    int i, upper = TRUE;
  482.  
  483.    for (i = 0; s[i] != '\0'; i++ )
  484.       {
  485.       if (upper )
  486.          {
  487.          s[i] = toupper(s[i] );
  488.          upper = FALSE;
  489.          }
  490.       else
  491.          {
  492.          s[i] = tolower(s[i] );
  493.          }
  494.       if (strchr(" ,.-;:" ,s[i] ) != NULL )
  495.          {
  496.          upper = TRUE;
  497.          }
  498.       }
  499.    return(s );
  500. }
  501.  
  502. /*
  503.  *---------------------------------------------------------------------------
  504.  *
  505.  */
  506. char *trim(char *s )
  507. {
  508.    int i = 0;
  509.  
  510.    while (s[i] == ' ' )
  511.       i++;
  512.  
  513.    if (i != 0 )
  514.       {
  515.       strcpy(s ,&s[i] );
  516.       }
  517.    i = strlen(s );
  518.    if (i != 0 )
  519.       {
  520.       i--;
  521.       while (s[i] == ' ' && i >= 0 )
  522.          {
  523.          s[i] = '\0';
  524.          }
  525.       }
  526.    return(s );
  527. }
  528.  
  529.